const express = require('express')
const bodyParser = require('body-parser')
const config = require('./config')

const gitlabSearchRouter = require('./router/gitlab/search')
const tokenManagerRouter = require('./router/token')
const tspMockServiceRouter = require('./router/tsp/service')
const tspMockTableRouter = require('./router/tsp/table')
const oauthTokenRouter = require('./router/tsp/token')
const cseRouter = require('./router/cse')
const appidRouter = require('./router/appid')
const uidRouter = require('./router/uid')

const router = express.Router()
router.use((req, res, next) => {
  // TODO: do something before request
  next()
})

const app = express()
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))

/** ************************************************
 * begin `wisecar`
 ************************************************ **/
app.use('/wisecar', express.static('../dist'))

/**
 * `/wisecar/gitlab//search/assemble`
 * `/wisecar/gitlab//search/issues`
 * `/wisecar/gitlab//search/notes`
 */
app.use('/wisecar/gitlab', gitlabSearchRouter)
/**
 * `/wisecar/token/user`
 * `/wisecar/token/app`
 */
app.use('/wisecar/token', tokenManagerRouter)

/** ************************************************
 * end /wisecar
 ************************************************ **/

/** ************************************************
 * begin `tspmock`
 ************************************************ **/

app.use('/wisecar/tspmock/service', tspMockServiceRouter)
app.use('/wisecar/tspmock/table', tspMockTableRouter)
app.use('/wisecar/tspmock/oauth', oauthTokenRouter)

/** ************************************************
 * end `tspmock`
 ************************************************ **/

/** ************************************************
 * begin `cse`
 ************************************************ **/

app.use('/wisecar/cse', cseRouter)

/** ************************************************
 * end `cse`
 ************************************************ **/

/** ************************************************
 * begin `appid`
 ************************************************ **/

app.use('/wisecar/appid', appidRouter)

/** ************************************************
 * end `appid`
 ************************************************ **/

/** ************************************************
 * begin `uid`
 ************************************************ **/

app.use('/wisecar/uid', uidRouter)

/** ************************************************
 * end `uid`
 ************************************************ **/

app.listen(config.PORT, () => {
  console.log(`Faster wise car is running at ${config.PORT}`)
})

